package com.autotest0211;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * @author krystal
 * @date 2023/3/1 16:57
 */
public class autotest {
    ChromeDriver driver=new ChromeDriver();
    void implicitlyWait(){
        //添加隐式等待
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.get("https://www.baidu.com");
        driver.findElement(By.cssSelector("#kw")).sendKeys("都暻秀");
        driver.findElement(By.cssSelector("#su")).click();
        driver.findElement(By.cssSelector("#\\31  > div > div > div > div > div.cos-row.row-text_Johh7.row_5y9Az > div > a > div > p > span > span"));
        driver.quit();
    }

    void webDriverWait(){
        driver.get("https://www.baidu.com");
        driver.findElement(By.cssSelector("#kw")).sendKeys("都暻秀");
        driver.findElement(By.cssSelector("#su")).click();
        //显示等待
        new WebDriverWait(driver,Duration.ofSeconds(5)).until(driver->driver.findElement(By.cssSelector("#\\31  > div > div > div > div > div.cos-row.row-text_Johh7.row_5y9Az > div > a > div > p > span > span")));
        driver.quit();
    }

    void navigateControl() throws InterruptedException {
        //浏览器导航
        //driver.get("https://www.baidu.com");
        driver.navigate().to("https://www.baidu.com");
        Thread.sleep(3000);
        //想要回退到访问百度网址之前的状态
        driver.navigate().back();
        Thread.sleep(3000);
        //前进，又进入到了百度首页
        driver.navigate().forward();
        Thread.sleep(3000);
        //刷新百度首页
        driver.navigate().refresh();
        Thread.sleep(3000);
        driver.quit();
    }

    void AlertControl(){
        //弹窗
        //get参数为url，而不是文件路径
        //在本地打开html文件之后，需要复制浏览器里的链接放到get里，而不是复制html本地的文件路径
        driver.get("xxx");
        //打开弹窗
        driver.findElement(By.cssSelector("xxx")).click();
        //切换到弹窗进行弹窗的处理
        Alert alert=driver.switchTo().alert();
        //1、点击确认
        alert.accept();
        //2、点击取消
        alert.dismiss();
        //3、输入关键词
        alert.sendKeys("xxx");
        driver.quit();
    }

    void selectControl() throws InterruptedException {
        //选择框
        driver.get("xxx");
        Thread.sleep(3000);
        WebElement ele=driver.findElement(By.cssSelector("xxx"));
        //先创建选择框对象
        Select select=new Select(ele);
        Thread.sleep(3000);
        //根据文本来选择
        select.selectByVisibleText("");
        //根据属性值来选择
        select.selectByValue("");
        //根据序号来选择
        select.selectByIndex(1);
        Thread.sleep(3000);
        driver.quit();
    }

    void scriptControl() throws InterruptedException {
        //执行脚本
//        driver.get("https://image.baidu.com/");
//        Thread.sleep(3000);
//        //执行js命令：让页面置顶/置底
//        driver.executeScript("document.documentElement.scrollTop=300");
//        Thread.sleep(3000);
//        driver.executeScript("document.documentElement.scrollTop=0");
        driver.get("https://www.baidu.com");
        Thread.sleep(3000);
        driver.executeScript("var d=document.querySelector('#kw');d.value='边伯贤'");
        Thread.sleep(3000);
        driver.quit();
    }

    void fileUploadControl() throws InterruptedException {
        driver.get("xxx");
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("xxx")).sendKeys("文件路径+文件");
        Thread.sleep(3000);
        driver.quit();
    }

    void paramControl(){
        //百度搜索边伯贤
        //先创建选项对象，在设置浏览器参数
        ChromeOptions options=new ChromeOptions();
        options.addArguments("-headless");
        ChromeDriver driver=new ChromeDriver(options);
        driver.get("https://www.baidu.com");
        driver.findElement(By.cssSelector("#kw")).sendKeys("边伯贤");
        driver.findElement(By.cssSelector("#su")).click();
        driver.quit();
    }
}
